<?php

require_once(APPPATH.'controllers/epicentr.php');

class Contact extends Epicentr {

	public function Contact() {
		parent::Epicentr();
		
		switch ($this->authentication->getProfileType()) {
			case 'admin':
			$this->permissions->addcontact = true;
			$this->permissions->editcontact = true;
			break;
			case 'standard':
			$this->permissions->addcontact = false;
			$this->permissions->editcontact = false;
			break;
			case 'client':
			$this->permissions->addcontact = true;
			$this->permissions->editcontact = true;
			break;
		}
		
	}
	
	public function showContactDetail($contactid) {
		$this->load->model('contactmodel');
		//should really load a pagelet but we cant just now!
	
		$contact = $this->contactmodel->retrieve_by_pkey($contactid);
		$output_string = "<strong>Postion:</strong> ".$contact->contact_title."<br />".
						"<strong>Email Address:</strong> ".$contact->email1."<br />".
						"<strong>DDI:</strong> ".$contact->ddi."<br />".
						"<strong>Mobile:</strong> ".$contact->mobile."<br />";
		
		echo $output_string;
	}
	
	public function contactVcard($contactid = 0) {
		$this->load->model('contactmodel');
		$this->load->model('companiesmodel');
		$this->load->library('vcard');
		
		$contact = $this->contactmodel->retrieve_by_pkey($contactid);
		$company = $this->companiesmodel->getCurrentCompanyForContact($contact->id);
		
		$this->vcard->data['first_name'] = $contact->firstname;
		$this->vcard->data['last_name']  = $contact->surname;
		$this->vcard->data['role']  = $contact->contact_title;
		$this->vcard->data['office_tel']  = $contact->ddi;
		$this->vcard->data['cell_tel']  = $contact->mobile;
		$this->vcard->data['email1']  = $contact->email1;
		$this->vcard->data['email2']  = $contact->email2;
		$this->vcard->data['company']  = $company->company_name;
		
		$this->vcard->build();
		$this->vcard->download();exit();
	}
	
	public function setContactAccess($contactid) {
		$this->load->model('contactmodel');
		$this->load->model('usersmodel');

		$contact = $this->contactmodel->retrieve_by_pkey($contactid);
		
		$company = $this->contactmodel->getCurrentCompany($contact->id);
		
		if (!$contact->usersid ) {
			// User has NO account associated
			
			$rand_pw = base_convert(rand(0, PHP_INT_MAX) . rand(0, PHP_INT_MAX), 10, 36);
			
			$data->password		= md5($rand_pw);
			$data->emailaddress	= $contact->email1;
			$data->firstname	= $contact->firstname;
			$data->lastname		= $contact->surname;
			$data->profiletype	= 'client';
			$data->active		= 1;
			
			$data->datecreated = date('Y-m-d H:i:s');
			$data->creatorid = $this->authentication->getUserId();

			$newid = $this->usersmodel->add($data);
			
			$this->load->library('email');

			$this->email->from($this->authentication->getUserEmailaddress(), $this->authentication->getUserFullName());
			$this->email->to($contact->email1); 
			
			$this->email->subject('Epicentr Access');
			$this->email->message("You have been given access to see your projects on epicentr at
			
".site_url()."

			Please login with your email address (".$contact->email1.")  and the password ".$rand_pw."
			");	
			
			$this->email->send();
			
			$this->db_session->set_flashdata('flashmessage', $contact->firstname.' '.$contact->surname.' has been emailed with their password to access epicentr.');
			
		} else {
			
			$this->db_session->set_flashdata('flashmessage', $contact->firstname.' '.$contact->surname.' already has access to epicentr.');
			
		}
    	
    	redirect('companies/view/'.$company->id, 'location');
    }
    
    public function removeContactAccess($contactid) {
		$this->load->model('contactmodel');
		$this->load->model('usersmodel');

		$contact = $this->contactmodel->retrieve_by_pkey($contactid);
		
		$company = $this->contactmodel->getCurrentCompany($contact->id);
		
		if ($contact->usersid) {
			// User has account associated
			$data->deleted = 1;
			$data->dateupdated = date('Y-m-d H:i:s');
			$data->updaterid = $this->authentication->getUserId();

			$this->usersmodel->modify($contact->usersid, $data);
						
			$this->db_session->set_flashdata('flashmessage', $contact->firstname.' '.$contact->surname.' has been emailed with their password to access epicentr.');
			
		} else {
			
			$this->db_session->set_flashdata('flashmessage', $contact->firstname.' '.$contact->surname.' already has access to epicentr.');
			
		}
    	
    	redirect('companies/view/'.$company->id, 'location');
    }
	
	public function delete($contactid = 0) {
		$this->load->model('contactmodel');
		$this->load->model('companiesmodel');
		
		$contact = $this->contactmodel->retrieve_by_pkey($contactid);
		$company = $this->companiesmodel->getCurrentCompanyForContact($contact->id);
		
		$this->contactmodel->pseudoDelete($contact->id);
		
		$undo_url = site_url('contact/undelete/'.$contact->id);
		
		$this->db_session->set_flashdata('flashmessage', $contact->firstname.' '.$contact->surname.' from '.$company->company_name.' was deleted. <a href="'.$undo_url.'">Undo</a>');
		
		
		redirect('companies/view/'.$company->id);
	}
	
	public function undelete($contactid = 0) {
		$this->load->model('contactmodel');
		$this->load->model('companiesmodel');
		
		$contact = $this->contactmodel->retrieve_by_pkey($contactid);
		$company = $this->companiesmodel->getCurrentCompanyForContact($contact->id);
		
		$this->contactmodel->pseudoUndelete($contact->id);
		
		$undo_url = site_url('contact/delete/'.$contact->id);
		
		$this->db_session->set_flashdata('flashmessage', $contact->firstname.' '.$contact->surname.' from '.$company->company_name.' was undeleted. <a href="'.$undo_url.'">Redo</a>');
		
		
		redirect('companies/view/'.$company->id);
	}
	
	public function addcontact() {
		$this->load->model('contactmodel');
		
		$this->load->model('basemodel');
    	
    	$companyid = $this->input->post('companyid');
    	
    	$data = null;
    	$data->datecreated = date('Y-m-d H:i:s');
		$data->creatorid = $this->authentication->getUserId();
    	
    	
   		$contactid = $this->contactmodel->add($data);
   		
   		$this->basemodel->setModel('company_contact');
   		$this->basemodel->add(array('contactid' => $contactid, 'companyid' => $companyid, 'current_company' => 1));
    	
    	
    	
    	$contact = $this->contactmodel->retrieve_by_pkey($contactid);
    	
    	
		$contacthtml = $this->load->view('epicentr/content/companies/contact', array('contact' => $contact, 'permissions' => $this->permissions), true);
		
		include_once ('Zend/Json.php');
		echo Zend_Json::encode( array('status' => 'ok', 'html' => $contacthtml, 'js' => '') );exit();
	}
	
}
?>